package org.eleorc.resources.builtinresource;

import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.io.InputStream;

public class ClassPathResourceExample {

    public static void main(String[] args) {
        // Create a ClassPathResource instance without specifying class loader or class
        ClassPathResource resource = new ClassPathResource("application.properties");

        try {
            // Check if the resource exists
            if (resource.exists()) {
                System.out.println("Resource exists");

                // Get an InputStream to read the resource
                try (InputStream inputStream = resource.getInputStream()) {
                    // 读取文件内容并打印
                    byte[] buffer = new byte[inputStream.available()];
                    inputStream.read(buffer);
                    System.out.println(new String(buffer));
                }
            } else {
                System.out.println("Resource does not exist");
            }
        } catch (IOException e) {
            System.err.println("Error occurred while accessing resource: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
